<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\result\Result;
use app\model\Permission as ModelPermission;
use think\facade\Request;

class Permission extends BaseController
{

    protected $middleware = [
        'auth',
        'checkPermission'

    ];

    public function index()
    {
        $pageNum = Request::param('pageNum');
        $pageSize = Request::param('pageSize');
        $permissionList = ModelPermission::page($pageNum, $pageSize)->select();
        return Result::Success($permissionList);
    }

    public function add()
    {
        $name = Request::post('name');
        $path = Request::post('path');

        $hasPermission = ModelPermission::where('name', $name)->findOrEmpty();
        if ($hasPermission->isEmpty()) {

            $permission = new ModelPermission();
            $permission->name = $name;
            $permission->path = $path;

            $permission->save();

            return Result::Success($permission);
        } else {
            return Result::Error(500, '同名权限已存在');
        }
    }
}
